﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using eventr.View;
using System.Windows.Data;
using System.Windows.Controls.Primitives;
using System.Windows.Browser;
using System.Threading;
using System.Windows.Controls.Theming;
using eventr.Controls;

namespace eventr.View
{
    public partial class MainPage : UserControl
    {

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            MainPageViewModel model = new MainPageViewModel(SuperDeepZoomer);
            model.SearchCompleted += new EventHandler(model_SearchCompleted);
            DataContext = model;
            if (HtmlPage.Document.QueryString.Keys.Contains("k") && !string.IsNullOrEmpty(HtmlPage.Document.QueryString["k"]))
                SearchFor(HtmlPage.Document.QueryString["k"]);
        }

        void model_SearchCompleted(object sender, EventArgs e)
        {
            SearchingIndicator.IsBusy = false;
            //have to wait a little bit before calling arrange images... otherwise it doesn't work
            ThreadPool.QueueUserWorkItem((state) =>
            {
                Thread.Sleep(500);
                Dispatcher.BeginInvoke(() => SuperDeepZoomer.ShowAllImages());
            });
        }

        private void Slideshow_Next(object sender, EventArgs e)
        {
            SuperDeepZoomer.NextSlide();
        }

        private void Slideshow_Previous(object sender, EventArgs e)
        {
            SuperDeepZoomer.PrevSlide();
        }

        private void MainMenu_ZoomIn(object sender, EventArgs e)
        {
            SuperDeepZoomer.ZoomIn();
        }

        private void MainMenu_ZoomOut(object sender, EventArgs e)
        {
            SuperDeepZoomer.ZoomOut();
        }

        private void MainMenu_NavigateHome(object sender, EventArgs e)
        {
            SuperDeepZoomer.ShowAllImages();
        }

        private void MainMenu_ToggleFullScreen(object sender, EventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        private void MainMenu_ToggleSlideShow(object sender, EventArgs e)
        {
            if (SuperDeepZoomer.SlideshowRunning)
            {
                SuperDeepZoomer.StopSlideshow();
            }
            else
            {
                SuperDeepZoomer.StartSlideshow();
            }
        }

        bool viewCurrentFlyThrough = false;

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F)
            {
                if (!viewCurrentFlyThrough)
                {
                    SuperDeepZoomer.SwitchOverlaySource(SuperDeepZoom.FlyThroughSource.FromCurrentUser);

                    //this will break any binding that has been previously set for this property
                    //this is okay as the binding has performed it's duty by this time in *this*
                    //project, but just be wary of doing this.
                    SuperDeepZoomer.FlyThroughOverlayEnabled = true;
                }
                else
                {
                    SuperDeepZoomer.FlyThroughOverlayEnabled = false;
                }
                viewCurrentFlyThrough = !viewCurrentFlyThrough;
            }
            else if (e.Key == Key.P)
            {
                if (SuperDeepZoomer.SlideshowRunning)
                {
                    SuperDeepZoomer.StopSlideshow();
                }
                else
                {
                    SuperDeepZoomer.PlayFlyThrough(TimeSpan.FromSeconds(2));
                }
            }
            else if (e.Key == Key.G)
            {
                ViewModel.Commands["ToggleMostViewed"].Execute();
            }
        }

        private void FlyThroughInfoPopup_PlayFlyThrough(object sender, EventArgs e)
        {
            SuperDeepZoomer.HideFlyThroughOverlay();
            SuperDeepZoomer.PlayFlyThrough(TimeSpan.FromSeconds(5));
        }

        private void FlyThroughPopup_HideFlyThrough(object sender, EventArgs e)
        {
            SuperDeepZoomer.HideFlyThroughOverlay();
        }

        //private void SearchTerm_KeyUp(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Enter)
        //    {
        //        SearchFor(SearchTerm.Text);
        //    }
        //}

        private void SearchFor(string term)
        {
            MainPageViewModel dc = DataContext as MainPageViewModel;
            dc.CurrentSearchTerm = term;
            //wipe out the refiners since the user is starting a new query using search term
            dc.SelectedRefinementValues.Clear();
            SearchingIndicator.IsBusy = true;
            dc.Refresh();
        }

        private bool _leftPanelIsHidden = true;

        private void SetLeftPanelState()
        {
            if (_leftPanelIsHidden)
                LeftPanelHide.Begin();
            else
                LeftPanelShow.Begin();
        }

        private void MainMenu_NextImage(object sender, EventArgs e)
        {
            SuperDeepZoomer.NextSlide();
        }

        private void MainMenu_PreviousImage(object sender, EventArgs e)
        {
            SuperDeepZoomer.PrevSlide();
        }

        //private void btnSearch_Click(object sender, RoutedEventArgs e)
        //{
        //    SearchFor(SearchTerm.Text);
        //}

        private void btnRefinement_Click(object sender, RoutedEventArgs e)
        {
            _leftPanelIsHidden = !_leftPanelIsHidden;
            SetLeftPanelState();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DefaultComboBox box = sender as DefaultComboBox;
            MainPageViewModel dc = DataContext as MainPageViewModel;
            if (dc != null && !dc.IsSearching && !box.IsSettingDefault)
            {
                ComboBox cb = sender as ComboBox;

                foreach (Model.RefinementValue addedVal in e.AddedItems)
                {
                    if (!dc.SelectedRefinementValues.Any(rv => rv.Token == addedVal.Token))
                        dc.SelectedRefinementValues.Add(addedVal);
                }

                foreach (Model.RefinementValue removedVal in e.RemovedItems)
                {
                    Model.RefinementValue foundVal = dc.SelectedRefinementValues.FirstOrDefault(rv => rv.Token == removedVal.Token);
                    if (foundVal != null)
                        dc.SelectedRefinementValues.Remove(foundVal);
                }

                dc.Refresh();
            }
        }
    }
}
